<?php
/**
 * 钻石投票-报名
 *
 * @author 天涯织梦
 * @url http://bbs.we7.cc/
 */

defined('IN_IA') or exit('Access Denied');
global $_W,$_GPC;
$uniacid = intval($_W['uniacid']);
$userinfo=mc_oauth_userinfo();
if(empty($userinfo)){
  message("抱歉，仅能在微信中打开！"); 
}
$rid=intval($_GPC['rid']);
$reply = pdo_fetch("SELECT * FROM " . tablename($this->tablereply) . " WHERE rid = :rid ORDER BY `id` DESC", array(':rid' => $rid));
$reply=array_merge ($reply,unserialize($reply['config']));unset($reply['config']);


//是否关注
//是否关注
if($_W['fans']['follow']!=1 && $reply['isfollow']>=1){
	$nofollow=1;
}
//地区是否限制

//活动是否过期


$voteuser = pdo_fetch("SELECT * FROM " . tablename($this->tablevoteuser) . " WHERE rid = :rid AND  openid = :openid ORDER BY `id` DESC", array(':rid' => $rid,':openid' => $userinfo['openid']));

if(!empty($voteuser)){
	Header("Location:".$this->createMobileUrl('view', array('rid' => $rid,'id' => $voteuser['id'])));
}

if($_W['ispost']){
	//是否关注
	if($_W['fans']['follow']!=1 && $reply['isfollow']>=1){
		exit(json_encode(array('status' => '500', 'msg' => "没有关注")));
	}
	if(empty($voteuser)){
		for ($x=0; $x<count($_GPC['picturearr']); $x++) {
		  $img[]=$this->upimages($_GPC['picturearr'][$x]);
		}  
		$joindata = array(
			'rid'=>$rid,
			'uniacid'=>$_W['uniacid'],
			'openid'=>$userinfo['openid'],
			'avatar' =>$userinfo['avatar'],
			'nickname'=>$userinfo['nickname'],
			'user_ip'=>$_W['clientip'],
			'sex'=>$_GPC['sex'], 
			'name'=>$_GPC['name'],
			'mobile' =>$_GPC['mobile'],
			'introduction' =>$_GPC['introduction'],
			'img1'=>$img[0],
			'img2'=>$img[1],
			'img3'=>$img[2],
			'img4'=>$img[3],
			'img5'=>$img[4],
			'formatdata'=>iserializer($_GPC['picturearr']),
			'votenum'=>0,
			'diamondnum'=>0,
			'status'=>0,
			'createtime'=>time()
		);
		pdo_insert($this->tablevoteuser, $joindata);
		$insertid=pdo_insertid();
		if($insertid){
			exit(json_encode(array('status' => '1', 'errmsg' => "成功",'id'=>$insertid)));
		}else{
			exit(json_encode(array('status' => '0', 'errmsg' => "发生错误，请重试！")));
		}
	}else{
		exit(json_encode(array('status' => '1', 'errmsg' => "已报名过",'id'=>$voteuser['id'])));
	}
}

include $this->template('join');
